import React, { useState, useEffect } from 'react'
import {Input, message} from "antd";
import {fetchRequest} from "../../../utils/Request";
import {url} from "../../../utils/url";
import {useRequest} from "@umijs/hooks";

interface searchParams {
    accDeviceCode: string
}

interface deviceState {
    state: string,
}

export default function EquipmentState() {

    const [searchParams, setSearchParams] = useState<searchParams>({ accDeviceCode: '' });
    const [deviceState, setDeviceState] = useState<deviceState>({ state: "" });

    const { Search } = Input;

    const onSearch = (value:string) => {
        if(value === ""){
            message.error("不允许输入空值");
        }
        else{
            getDeviceState.run({accDeviceCode:value});
        }
    }

    const getDeviceState = useRequest(getDeviceStateRequest, {
        manual: true,
        loadingDelay: 200
    })

    //请求函数
    async function getDeviceStateRequest(params: searchParams) {
        // console.log(params)
        try {
            const json = await fetchRequest(url.GET_DEVICE_STATE, params)
            if (json.success) {
                setDeviceState({state:json.data});
                if(json.data === "在线"){
                    message.success("设备在线");
                }
                else if(json.data === "离线"){
                    message.error("设备离线");
                }
                else
                    message.info(json.data);
            } else {
                message.destroy();
                message.error(json.errorMsg);
            }
        } catch (error:any) {
            message.destroy();
            message.error(error.message);
        }
    }

    return (
        <div>
            <Search
                style={{ margin: '20px', padding: "24px 0 0 0 ",width:'60%' }}
                placeholder="请输入设备编码"
                allowClear
                enterButton="搜索"
                size="large"
                onSearch={onSearch}
                loading={getDeviceState.loading}
            />
        </div>

    )

}